<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <script src="/static/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <title>Java多文件截取帧图片 @yc</title>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>多个文件截取帧图片</legend>
</fieldset>
<div class="layui-collapse">
    <div class="layui-colla-item">
        <h2 class="layui-colla-title layui-elem-quote">参数设置</h2>
        <div class="layui-colla-content layui-show">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width: 100px;">截取第几帧</label>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="text" name="number" id="number" lay-verify="required|number"
                                   autocomplete="off" value="5"
                                   class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">建议5~299</div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width: 100px;">图片存放目录</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-verify="required"
                                   autocomplete="off" readonly value=" 桌面/(当前日期)/文件名.png"
                                   class="layui-form-mid  layui-word-aux">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width: 100px;">等比例缩放</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-verify="required"
                                   autocomplete="off" readonly value=" 原始比例大小"
                                   class="layui-form-mid  layui-word-aux">
                        </div>
                    </div>

                </div>
                <div class="layui-form-item">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn layui-btn-normal"
                                id="testList"><i class="layui-icon"></i>选择多文件
                        </button>
                        <div class="layui-upload-list">
                            <table class="layui-table">
                                <thead>
                                <tr>
                                    <th>文件名</th>
                                    <th>大小</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="demoList"></tbody>
                            </table>
                        </div>
                        <button type="button" class="layui-btn" id="testListAction">开始上传
                        </button>
                        <div class="layui-input-inline">
                            <input type="hidden" id="presesText"
                                   autocomplete="off" readonly value="0%"
                                   class="layui-form-mid  layui-word-aux">
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script th:inline="javascript">
    var $, form, element, upload, layer;
    var codeMap = {};  //传递到后端的名称
    layui.use(['form', 'element', 'upload', 'layer'], function () {
        element = layui.element;
        upload = layui.upload;
        form = layui.form;
        layer = layui.layer;
        $ = layui.jquery;


        //多文件列表示例
        var demoListView = $('#demoList')
            , uploadListIns = upload.render({
            elem: '#testList'
            , url: '/user/upload'
            , accept: 'file' //普通文件  |video|audio
            , acceptMime: 'video/*'
//            exts: 'zip|rar|7z' //只允许上传压缩文件
//            , size: 60 //限制文件大小，单位 KB
            , multiple: true
            , auto: false  //选完文件后不自动上传
            , bindAction: '#testListAction'
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                codeMap = {};
                demoListView.html(" ");
                for (var keyId in files) {
                    var fileVal = files[keyId];
//                    console.log(keyId + "-----" + fileVal);
                    var tr = $(['<tr id="upload-' + keyId + '">'
                        , '<td>' + fileVal["name"] + '</td>'
                        , '<td>' + (fileVal["size"] / 1024 / 1024).toFixed(2) + 'M</td>'
                        , '<td>等待上传</td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete" tag="' + keyId + '">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    files[keyId] = fileVal.slice(0,  1024 * 1024, keyId); //传递名称
                    codeMap[keyId] = fileVal["name"];
                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        layer.msg("开始重传");
                        obj.upload(keyId, files[keyId]);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete codeMap[$(this).attr("tag")];
                        delete files[$(this).attr("tag")]; //删除对应的文件
                        $("#upload-" + $(this).attr("tag")).remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });
                    demoListView.append(tr);
                }
            }
            , data: {
                number: function () {
                    return $('#number').val();
                },
                codeMap: function () {
//                    console.log(codeMap);
                    return JSON.stringify(codeMap)
                }
            }
            , before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                layer.load(2, {time: 600});
            }
            , done: function (res, index, upload) {
                if (res.status == 200) { //上传成功
                    var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">处理成功,请到图片存放目录查看</span>');
                    tds.eq(3).html(''); //清空操作
                    delete codeMap[index];
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                layer.msg(res.msg);
                this.error(index, upload);
            }
            , progress: function (n) {
                var percent = '进度: ' + n + '%'; //获取进度百分比
                $("#presesText").attr("type", "text");
                $("#presesText").val(percent);
            }
            , allDone: function (obj) { //当文件全部被提交后，才触发
                layer.msg("处理完毕!");
                $("#presesText").attr("type", "hidden");
                console.log("总文件数:" + obj.total + ",请求成功的文件数:" + obj.successful + ",请求失败的文件数" + obj.aborted);
            }
            , error: function (index, upload) {
                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });
    });
</script>

</body>
</html>